from time import time

def bubble(A):
    for j in range(len(A) - 1, -1, -1):
        for i in range(j):
            if A[i] > A[i + 1]:
                t = A[i]
                A[i] = A[i + 1]
                A[i + 1] = t

def bubble_2(A):
    A_is_sorted = False
    N = len(A) - 1
    while N > 0 and not A_is_sorted:
        A_is_sorted = True
        for i in range(N):
            if A[i] > A[i + 1]:
                t = A[i]
                A[i] = A[i + 1]
                A[i + 1] = t
                A_is_sorted = False
        N -= 1

def count_time(array, sort_func):
    begin_time = time()
    sort_func(array)
    end_time = time()
    return int((end_time - begin_time) * 1000)

for filename in ('m1_sorted.txt', 'm2_reverse.txt', 'm3_shuffle.txt'):
    f = open(filename, 'r')
    data = [int(x) for x in f]
    f.close()
    data2 = data.copy()
    sort_time1 = count_time(data, bubble)
    sort_time2 = count_time(data, bubble_2)
    print(filename, "is sorted by", sort_time1, "with bubble sort")
    print(filename, "is sorted by", sort_time2, "with bubble sort")
    
